package com.example.demo.zaqizabatest.leetcode.day2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) { val = x; }
 * }
 *
 * 输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。
 */
public class Solution1 {

    public int[] reversePrint(ListNode head) {
        if (head != null){
            List<Integer> list = new ArrayList<>();
            list.add(head.val);
            ListNode nextNode = head.next;
            while (nextNode != null) {
                list.add(nextNode.val);
                nextNode = nextNode.next;
            }
            Collections.reverse(list);
            return list.stream().mapToInt(Integer::intValue).toArray();
        }
        return new int[0];
    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
